package com.kehutong.charge.admin;

import com.kehutong.charge.dto.ChargeStatistDTO;
import com.kehutong.charge.entity.ChargeHouseConfig;
import com.kehutong.charge.entity.ChargeHouseSet;
import com.kehutong.charge.service.ChargeHouseService;
import com.kehutong.charge.util.enums.ResidentsTypeEnum;
import com.kehutong.common.DeptService;
import com.kehutong.common.PageUtils;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.enums.ResidentType;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;

import java.util.List;

/**
 * @program: kht
 * @description: 收费房屋配置/收费配置
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/house")
public class ChargeHouseController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeHouseController.class);

    @Inject
    DeptService deptService;

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    ChargeHouseService chargeHouseService;



    @ReqMapping("/page")
    @HasPermission("pay:charge:house:view")
    public Object page(JSONObject jsonObject) throws Exception{
        return chargeHouseService.findPage(jsonObject);
    }

    @ReqMapping("/list")
    @HasPermission("pay:charge:house:view")
    public Object list(JSONObject jsonObject) {
        return chargeHouseService.findList(jsonObject);
    }


    @ReqMapping("/config/list")
    public Object listByMultiple(JSONObject jsonObject) {
        return chargeHouseService.listByMultiple(jsonObject);
    }


    @ReqMapping("/listSet")
    @HasPermission("pay:charge:house:view")
    public Object listSet(JSONObject jsonObject) {
        return chargeHouseService.findSetList(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:house:edit")
    @ReqMapping("/save")
    public Object save(ChargeHouseConfig entity, Token token) throws Exception {
        entity.setCreateName(token.getUsername());
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }

        return chargeHouseService.save(entity);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:house:edit")
    @ReqMapping("/saveChargeHouseSet")
    public Object saveChargeHouseSet(ChargeHouseSet entity, Token token,JSONObject jsonObject){
        String reType = jsonObject.getString("reType");
        entity.setUpdateName(token.getUsername());
        entity.setCreateName(token.getUsername());
        entity.setReType(ResidentsTypeEnum.getEnum(reType));
        return chargeHouseService.saveSet(entity);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:house:edit")
    @ReqMapping("/deleteChargeHouseSet")
    public Object deleteChargeHouseSet(ChargeHouseSet entity, Token token) throws Exception {
        return chargeHouseService.deleteChargeHouseSet(entity);
    }


    @Bean(copy=true)
    @HasPermission("pay:charge:house:edit")
    @ReqMapping("/update")
    public Object update(JSONObject jsonObject, ChargeHouseConfig entity) throws Exception {

        return chargeHouseService.updateHouseSet(entity,jsonObject);
    }


    @HasPermission("pay:charge:house:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeHouseConfig entity) {

        return chargeHouseService.deleteById(entity.getId());
    }

    @ReqMapping("/get")
    public Object get(ChargeHouseConfig entity) {
        return chargeHouseService.get(entity);
    }

    @ReqMapping("/getDetail")
    public Object getDetail(JSONObject jsonObject) {

        return chargeHouseService.findFeeDetail(jsonObject);
    }

    @HasPermission("pay:charge:house:edit")
    @ReqMapping("/setStatus")
    public Object setStatus(ChargeHouseConfig entity) {
        entity.setConfigStatus(!entity.isConfigStatus());
        return chargeHouseService.setStatus(entity);
    }
    /**
     * 统计功能
     */
    @ReqMapping("/statist")
    @HasPermission("pay:charge:house:edit")
    public Object statist(JSONObject jsonObject,Token token) throws Exception {

        List<ChargeStatistDTO> listObj = chargeHouseService.findStatistPage(token, jsonObject);

        return PageUtils.buildPage(jsonObject,listObj);
    }

    /**
     * 统计功能
     */
    @ReqMapping("/statistnew")
    @HasPermission("pay:charge:house:view")
    public Object statistnew(JSONObject jsonObject,Token token) throws Exception {
        List<ChargeStatistDTO> listObj = chargeHouseService.findHouseStatistPage(token, jsonObject);
        return PageUtils.toBuildPage(jsonObject,listObj);
    }

    /**
     * 小区数据同步功能
     */
    @ReqMapping("/synHouseData")
    public Object synHouseData(JSONObject jsonObject,Token token) throws Exception {

        return chargeHouseService.synHouseData(jsonObject,token);
    }

    @HasPermission("pay:charge:house:view")
    @ReqMapping("/getMeterManageByHouse")
    public Object getMeterManageByHouse(JSONObject jsonObject) {
        return chargeHouseService.getMeterManageByParam(jsonObject);
    }


    @HasPermission("pay:charge:house:view")
    @ReqMapping("/getChargeHouseSetByHouse")
    public Object getChargeHouseSetByHouse(JSONObject jsonObject) {
        return chargeHouseService.getChargeHouseSetBychargeHouseNo(jsonObject);
    }
}
